<template>
  <!-- 导入表 -->
  <el-dialog v-model="visible" title="导入表" width="800px" top="5vh" append-to-body>
    <el-form :model="queryParams" ref="queryFormRef" size="small" :inline="true">
      <el-form-item label="表名称" prop="tableName">
        <el-input
          v-model="queryParams.tableName"
          placeholder="请输入表名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="表描述" prop="tableComment">
        <el-input
          v-model="queryParams.tableComment"
          placeholder="请输入表描述"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-table
        ref="tableRef"
        :data="dbTableList"
        @selection-change="handleSelectionChange"
        height="260px"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column
          prop="tableName"
          label="表名称"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          prop="tableComment"
          label="表描述"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column prop="updateTime" label="更新时间"></el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </el-row>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleImportTable">确 定</el-button>
        <el-button @click="visible = false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, defineEmits } from 'vue'
import { listDbTable, importTable } from '@/api/tool/gen'
import { ElMessage } from 'element-plus'
import type { FormInstance } from 'element-plus'

// 定义类型
interface Table {
  tableName?: string
  tableComment?: string
  createTime?: string
  updateTime?: string
}

interface QueryParams {
  pageNum: number
  pageSize: number
  tableName?: string
  tableComment?: string
}

// 引用
const queryFormRef = ref<FormInstance>()
const tableRef = ref()

// 数据
const visible = ref(false)
const tables = ref<string[]>([])
const total = ref(0)
const dbTableList = ref<Table[]>([])

const queryParams = reactive<QueryParams>({
  pageNum: 1,
  pageSize: 10,
  tableName: undefined,
  tableComment: undefined,
})

// 定义事件
const emit = defineEmits<{
  (e: 'ok'): void
}>()

// 方法
// 显示弹框
const show = () => {
  getList()
  visible.value = true
}

// 点击行
const clickRow = (row: Table) => {
  tableRef.value?.toggleRowSelection(row)
}

// 多选框选中数据
const handleSelectionChange = (selection: Table[]) => {
  tables.value = selection.map((item) => item.tableName || '')
}

// 查询表数据
const getList = () => {
  listDbTable(queryParams).then((res) => {
    if (res.code === 200) {
      dbTableList.value = res.rows
      total.value = res.total
    }
  })
}

// 搜索按钮操作
const handleQuery = () => {
  queryParams.pageNum = 1
  getList()
}

// 重置按钮操作
const resetQuery = () => {
  // 重置表单，这里简化处理
  queryParams.tableName = undefined
  queryParams.tableComment = undefined
  handleQuery()
}

// 导入按钮操作
const handleImportTable = () => {
  const tableNames = tables.value.join(',')
  if (tableNames == '') {
    ElMessage.error('请选择要导入的表')
    return
  }
  importTable({ tables: tableNames }).then((res) => {
    ElMessage.success(res.msg)
    if (res.code === 200) {
      visible.value = false
      emit('ok')
    }
  })
}

// 暴露方法
defineExpose({
  show,
})
</script>
